/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rps.controller;

import bean.entity.Applicants;
import bean.entity.Departments;
import bean.entity.Employees;
import bean.entity.Interviews;
import bean.entity.InterviewsPK;
import bean.entity.Vacances;
import bean.session.ApplicantSessionBean;
import bean.session.DepartmentSessionBean;
import bean.session.EmployeeSessionBean;
import bean.session.InterviewSessionBean;
import bean.session.VacancySessionBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 *
 * @author Vu Thang
 */
@ManagedBean
@SessionScoped
public class VacancyManagedBean {

    @EJB
    private InterviewSessionBean interviewSessionBean;
    @EJB
    private EmployeeSessionBean employeeSessionBean;
    @EJB
    private ApplicantSessionBean applicantSessionBean;
    @EJB
    private DepartmentSessionBean departmentSessionBean;
    @EJB
    private VacancySessionBean vacancySessionBean;
    private List<Vacances> findbyVacyTitleList;
    private Vacances va;
    private String param;
    private String mess;
    private String vacancyID;
    private String VacantyTitle;
    private String NumOfJob;
    private String Salary;
    private String Experience;
    private String StartDate;
    private String CloseDate;
    private String VaStatus;
    private String Departments;
    private String color;
    private String styleClass;
    private String rowClasses;
    private String headerClass;
    private Departments department;
    private int select;
    private String name;
    private String nameError;
    private List<SelectItem> vaRender;
    private Applicants app;
    private Interviews inter;
    private Employees emp;
    private List<SelectItem> listEmp;
    private InterviewsPK interPK;

    /** Creates a new instance of VacanceManagedBean */
    public VacancyManagedBean() {
        va = new Vacances();
        findbyVacyTitleList = new LinkedList<Vacances>();
        department = new Departments();
        vaRender = new ArrayList<SelectItem>();
        app = new Applicants();
        emp = new Employees();
        inter = new Interviews();
    }

    public Vacances getVacancyDetails() {
        return va;
    }

    public void setVacancyDetails(Vacances va) {
        this.va = va;
    }

    public Departments getDepartment() {
        return department;
    }

    public void setDepartment(Departments department) {
        this.department = department;
    }

    public List<Vacances> getAllVacancies() {

        findbyVacyTitleList = vacancySessionBean.findAllVacances();
        return findbyVacyTitleList;
    }

    public String findVacancyTitle() {
        findbyVacyTitleList = vacancySessionBean.findVacacyTitle(param);
        if (findbyVacyTitleList.size() <= 0) {
            vacancyID = null;
            VaStatus = null;
            VacantyTitle = null;
            NumOfJob = null;
            Salary = null;
            StartDate = null;
            CloseDate = null;
            Departments = null;
            Experience = null;
            color = "background-color: red";
            styleClass = null;
            rowClasses = null;
            headerClass = null;
            mess = "No resule !";
        } else {
            vacancyID = "id";
            VaStatus = "VaStatus";
            VacantyTitle = "VacantyTitle";
            NumOfJob = "NumOfJob";
            Salary = "Salary";
            StartDate = "StartDate";
            CloseDate = "CloseDate";
            Departments = "Departments";
            Experience = "Experience";
            styleClass = "joblist";
            rowClasses = "list_footer_cell";
            headerClass = "list_header_cell";
            mess = null;
        }
        return "pretty:searchresult";
    }

    public List<SelectItem> getListItemDepart() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        List<Departments> dp = departmentSessionBean.findAllDepart();
        items.add(new SelectItem(0, "---Select---"));
        if (dp == null || dp.size() > 0) {
            for (Departments item : dp) {
                items.add(new SelectItem(item.getDepartmentID(), item.getDepartmentName()));
            }
        }
        return items;
    }

    public void findIDVancancy(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        String name = (String) nameInput.getValue();
        if (name != null && select == 1) {
            findbyVacyTitleList = vacancySessionBean.findIdVacancy(name);
        } else {
            findbyVacyTitleList = vacancySessionBean.findVacacyTitle(name);
        }

    }

    public void setQuery(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        int ID = Integer.parseInt(nameInput.getValue().toString());
        select = ID;
    }

    public void updateStatus1(ValueChangeEvent evt) {
        vacancySessionBean.updateVacancy(findbyVacyTitleList);
        mUpdateVa="Change Status Successful !";
    }

    public void updateStatus(AjaxBehaviorEvent evt) {
        vacancySessionBean.updateVacancy(findbyVacyTitleList);
        mUpdateVa="Change Status Successful !";
    }

    String mUpdateVa;

    public String getmUpdateVa() {
        return mUpdateVa;
    }

    public void setmUpdateVa(String mUpdateVa) {
        this.mUpdateVa = mUpdateVa;
    }
    public String updateVacancy() {
        vacancySessionBean.updateVacancy(findbyVacyTitleList);
        return "pretty:hrgroup";
    }

//    public List<Vacances> pagingVacacesTable(){
//        return vacancySessionBean.pagingVacacesTable(2, 4);
//    }
    public List<Departments> getListdepart() {
        return departmentSessionBean.findAllDepart();
    }

    public String addVacancy() {

        try {
            Date d = new Date();
            va.setVacancyID("");
            va.setDepartments(department);
            va.setStartDate(d);
            va.setVaStatus(1);
            vacancySessionBean.addNewVacancy(va);
            findbyVacyTitleList = vacancySessionBean.findAllVacances();
            va.setDepartments(null);
            va.setVacantyTitle("");
            va.setNumOfJob(0);
            va.setSalary("");
            va.setExperience("");
            va.setStartDate(null);
            va.setCloseDate(null);
            va.setVaStatus(0);
        } catch (Exception e) {
            return null;
        }
        return "pretty:hrgroup";
    }

    public String confirm() {
        return "pretty:hrgroup";
    }

    public boolean isFindResult() {

        return findbyVacyTitleList != null;
    }

    public String getMess() {
        return mess;
    }

    public String showVacancyDetails(Vacances va) {
        this.va = va;
        return "pretty:vacancydetails";
    }

    public String Details(Vacances va) {
        this.va = va;
        return "pretty:details";
    }

    /**
     * @return the param
     */
    public String getParam() {
        return param;
    }

    /**
     * @param param the param to set
     */
    public void setParam(String param) {
        this.param = param;
    }

    /**
     * @return the findbyVacyTitleList
     */
    public List<Vacances> getFindbyVacyTitleList() {
        return findbyVacyTitleList;
    }

    /**
     * @param findbyVacyTitleList the findbyVacyTitleList to set
     */
    public void setFindbyVacyTitleList(List<Vacances> findbyVacyTitleList) {
        this.findbyVacyTitleList = findbyVacyTitleList;
    }

    /**
     * @return the vacancyID
     */
    public String getVacancyID() {
        return vacancyID;
    }

    /**
     * @return the VacantyTitle
     */
    public String getVacantyTitle() {
        return VacantyTitle;
    }

    /**
     * @param VacantyTitle the VacantyTitle to set
     */
    public void setVacantyTitle(String VacantyTitle) {
        this.VacantyTitle = VacantyTitle;
    }

    /**
     * @return the NumOfJob
     */
    public String getNumOfJob() {
        return NumOfJob;
    }

    /**
     * @return the Salary
     */
    public String getSalary() {
        return Salary;
    }

    /**
     * @return the Experience
     */
    public String getExperience() {
        return Experience;
    }

    /**
     * @return the StartDate
     */
    public String getStartDate() {
        return StartDate;
    }

    /**
     * @return the CloseDate
     */
    public String getCloseDate() {
        return CloseDate;
    }

    /**
     * @return the VaStatus
     */
    public String getVaStatus() {
        return VaStatus;
    }

    /**
     * @return the Departments
     */
    public String getDepartments() {
        return Departments;
    }

    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }

    /**
     * @return the styleClass
     */
    public String getStyleClass() {
        return styleClass;
    }

    /**
     * @return the rowClasses
     */
    public String getRowClasses() {
        return rowClasses;
    }

    /**
     * @return the headerClass
     */
    public String getHeaderClass() {
        return headerClass;
    }

    /**
     * @return the select
     */
    public int getSelect() {
        return select;
    }

    /**
     * @param select the select to set
     */
    public void setSelect(int select) {
        this.select = select;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the nameError
     */
    public String getNameError() {
        return nameError;
    }

    /**
     * @param nameError the nameError to set
     */
    public void setNameError(String nameError) {
        this.nameError = nameError;
    }

    public List<SelectItem> getVaRender() {
        return vaRender;
    }

    public void setVaRender(List<SelectItem> vaRender) {
        this.vaRender = vaRender;
    }

    public Applicants getApp() {
        return app;
    }

    public void setApp(Applicants app) {
        this.app = app;
    }

    public Employees getEmp() {
        return emp;
    }

    public void setEmp(Employees emp) {
        this.emp = emp;
    }

    public Interviews getInter() {
        return inter;
    }

    public void setInter(Interviews inter) {
        this.inter = inter;
    }

    public List<SelectItem> getListEmp() {
        return listEmp;
    }

    public void setListEmp(List<SelectItem> listEmp) {
        this.listEmp = listEmp;
    }

    public Vacances getVa() {
        return va;
    }

    public void setVa(Vacances va) {
        this.va = va;
    }

    public void renderPopup(ActionEvent e) {
        String parameter = (String) e.getComponent().getAttributes().get("demo");
        message = "";
        app = applicantSessionBean.findApplicantByID(parameter);
    }

    public void findValueChange(ValueChangeEvent e) {
        UIInput in = (UIInput) e.getComponent();
        vaRender = null;
        vaRender = new ArrayList<SelectItem>();
        listEmp = null;
        listEmp = new ArrayList<SelectItem>();
        String render = (String) in.getValue();
        List<Vacances> vacanceList = null;
        vacanceList = vacancySessionBean.findVacancyByDepartID(render);
        List<Employees> emps = null;
        emps = employeeSessionBean.findEmployeeByDepartID(render);
        if (vacanceList.size() > 0) {
            for (Vacances item : vacanceList) {
                vaRender.add(new SelectItem(item.getVacancyID(), item.getVacantyTitle()));
            }
        } else {
            vaRender = null;
            vaRender = new ArrayList<SelectItem>();
        }


        if (emps.size() > 0) {
            for (Employees item : emps) {
                listEmp.add(new SelectItem(item.getEmpNo(), item.getEmpNo()));
            }
        } else {
            listEmp = null;
            listEmp = new ArrayList<SelectItem>();
        }

    }

    public String addScheduleInterview() {
        try {
            interPK = new InterviewsPK(va.getVacancyID(), app.getApplicantID(), emp.getEmpNo());
            inter.setInterviewsPK(interPK);
            //get current date time with Date()
            Date date = new Date();
            inter.setAttachedDate(date);
            inter.setInStatus(0);
            inter.setApplicants(app);
            inter.setVacances(va);
            inter.setEmployees(emp);
            applicantSessionBean.updateOneStatus(app.getApplicantID(), 1);

            inter = interviewSessionBean.addNewInterview(inter);

            message = inter.getInterviewsPK().getApplicantID();
            return "pretty:hrgroup";
        } catch (Exception e) {
            return "";
        }
    }
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void findVacancyChange(ValueChangeEvent e) {
        UIInput in = (UIInput) e.getComponent();
        String render = (String) in.getValue();
        String appID = app.getApplicantID();
        String vaID = render;
        List<Interviews> litInter = new ArrayList<Interviews>();
        litInter = interviewSessionBean.findByAppIDAndVaID(appID);
        if (litInter != null) {
            for (Interviews interviews : litInter) {
                if (vaID.equals(interviews.getInterviewsPK().getVacancyID())) {
                    message = "This Vacancy was Attached";
                } else {
                    message = "";
                }
            }
        } else {
            message = "";
        }

    }
}
